package com.ruoyi.uitity;


import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

@Component
public class WxMessageProducer {

    private static final Logger log = LoggerFactory.getLogger(WxMessageProducer.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitAdmin rabbitAdmin;

    public String sendMessage(String msg, Long userId) {
        rabbitAdmin.purgeQueue(RabbitConfig.QUEUE_NAME, false);
        CorrelationData correlationId = new CorrelationData(userId.toString());

        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        map.put("userId", userId);

        String msgDemo = JSON.toJSONString(map);
        // 同步发送消息并等待响应
        String response = (String) rabbitTemplate.convertSendAndReceive(
                RabbitConfig.EXCHANGE_NAME,
                RabbitConfig.ROUTING_KEY,
                msgDemo,
                correlationId
        );
        return response;
    }
}
